package com.xianggu.agency.demo;

import com.xianggu.agency.jdkProxy.XiangGuProxy;
import com.xianggu.agency.jdkProxy.XiangGuProxyFactory;
import com.xianggu.agency.demo.impl.MyInterface2;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author chengjy
 * @date 2025/3/18
 */
public class ProxyDemo {
    public static void main(String[] args) throws IOException {
        System.out.println("------------正片开始------------");

        MyInterface2 proxyObject1 = (MyInterface2) XiangGuProxyFactory.createProxyObject(MyInterface2.class, new XiangGuProxy() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) {
                System.out.println("前置增强");
                try {
                    // 此处的obj传参不能是代理对象，否则会触发循环调用
                    method.invoke(proxy, args);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                } catch (InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
//                System.out.println("调用方法: " + method.getName() + "参数："+ Arrays.toString(args));
                System.out.println("后置增强");
                return null;
            }
        });
        proxyObject1.func1("1" , "2");
    }


}
